package com.mhs.BUS;

import java.util.List;

import android.content.Context;

import com.mhs.DAO.IRDAO;
import com.mhs.DAO.ThuocDAO;
import com.mhs.DTO.*;

public class ThuocBUS {

	public List<ThuocDTO> timKiemThuocBUS(String keyWord, int typeSearch, Context context){
		ThuocDAO thuocDAO = new ThuocDAO(context);
		return thuocDAO.timKiemThuocDAO(keyWord, typeSearch);
	}
	
	public boolean checkThuocExistAndInsert(ThuocDTO t, Context context){
		ThuocDAO thuocDAO = new ThuocDAO(context);
		return thuocDAO.checkThuocExistAndInsert(t);
	}
	
	public List<ThuocDTO> getAllThuocDTO(Context context){
		ThuocDAO thuocDAO = new ThuocDAO(context);
		return thuocDAO.getAllThuocDTO();
	}

	public boolean delThuocDTO(Integer idThuoc, Context context) {
		ThuocDAO thuocDAO = new ThuocDAO(context);
		return thuocDAO.delThuocDTO(idThuoc);
	}

	public boolean checkExistId(int ma, Context context) {
		ThuocDAO thuocDAO = new ThuocDAO(context);
		return thuocDAO.checkExistId(ma);
	}
	
	public boolean checkThuocCommand(ThuocDTO thuocDTO, Context context) {
		boolean checkThuocCommndAndExecute = false;
		ThuocDAO thuocDAO = new ThuocDAO(context);
		List<HLCDTO> lstIR = new IRDAO(context).getAllIRwithTableName("thuoc");
		if(lstIR.size() == 0){
			checkThuocCommndAndExecute = false;
		}else{
			for(HLCDTO ir : lstIR){
				if(thuocDTO.getMATHUOC() == Integer.parseInt(ir.getMa())){
					String sCommand = ir.getCommand();
					CommandQuery command = CommandQuery.valueOf(sCommand);
					switch(command){
					case insert:
						checkThuocCommndAndExecute = thuocDAO.inserthuocSync(thuocDTO);
						break;
					case update:
						checkThuocCommndAndExecute = thuocDAO.updateThuocSync(thuocDTO);
						break;
					case delete:
						checkThuocCommndAndExecute = thuocDAO.deleteThuocSync(thuocDTO);
						break;
						default:
							break;
					}
				}else{
					// do nothing
				}
			}	
		}
		return checkThuocCommndAndExecute;
	}
	
	public List<ThuocDTO> getAllInfoThuocDTO(Context context){
		ThuocDAO thuocDAO = new ThuocDAO(context);
		return thuocDAO.getAllInfoThuocDTO();
	}

	public ThuocDTO getThuocFromMaThuoc(Context context, int maThuoc) {
		ThuocDAO thuocDAO = new ThuocDAO(context);
		return thuocDAO.getThuocFromMaThuoc(maThuoc);
	}

	public boolean lockRecord(int maThuoc, Context context) {
		ThuocDAO thuocDAO = new ThuocDAO(context);
		return thuocDAO.lockRecord(maThuoc);
	}
}
